#!/bin/bash
#++++++++++++修改为dminit文件所在的绝对路径，具体为 ：达梦数据库的安装路径/bin/   
DMINIT_PATH="/home/henghui/dmdbms/bin"
#++++++++++++修改为此新建数据库的存放的位置，通常为/dmdata/data   
DATABASE_PATH="/dmdata/data"
#++++++++++++修改为allTable.sql文件的所在位置，
SQL_FILE=$(pwd)/allTables.sql

if [ -z "$1" ];
then
	echo "please set databasename！！！"
	exit 1
fi
if [ -z "$2" ];
then
	echo "please set database port number!!!"
	exit 1
fi

DM_NAME="$1"
#服务的后缀名 完整服务名: DmService + 后缀名
SERVICE_BEFOR_NAME="$DM_NAME"
PORT_NUM="$2"

# 初始化数据库实例
echo "Initializing database instance... "
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$DMINIT_PATH..."
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$DMINIT_PATH"
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$DMINIT_PATH..."
$DMINIT_PATH/dminit path=$DATABASE_PATH  PAGE_SIZE=32 EXTENT_SIZE=32 CASE_SENSITIVE=N CHARSET=1 DB_NAME=$DM_NAME INSTANCE_NAME=DBSERVER PORT_NUM=$PORT_NUM

# 注册数据库服务
cd  $DMINIT_PATH/../script/root/
echo "Registering database service.."
sudo ./dm_service_installer.sh \
    -t dmserver \
    -p $SERVICE_BEFOR_NAME \
    -dm_ini $DATABASE_PATH/$DM_NAME/dm.ini 

#启动数据库服务
cd $DMINIT_PATH
./DmService$SERVICE_BEFOR_NAME start

#创建数据库表
#<<COMMENT 执行sql文件，建用户+表
SQL_USERNAME="SYSDBA"
SQL_PASSWORD="SYSDBA"
#disql 在dm8安装目录/bin下
#.sql文件末尾必须加上exit，不然执行sql文件后停留在sql的终端里面
cd $DMINIT_PATH
./disql $SQL_USERNAME/$SQL_PASSWORD:$PORT_NUM \`$SQL_FILE 
#COMMENT
